package com.iot.manager.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.iot.manager.R;
import com.iot.manager.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import okhttp3.ResponseBody;

import org.json.JSONObject;

public class SendCommandDialog extends Dialog {
    private final String deviceId;
    private TextInputEditText etCommand;
    private TextInputEditText etParams;
    private MaterialButton btnSend;
    private MaterialButton btnCancel;

    public SendCommandDialog(@NonNull Context context, String deviceId) {
        super(context);
        this.deviceId = deviceId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_send_command, null);
        setContentView(view);

        etCommand = view.findViewById(R.id.et_command);
        etParams = view.findViewById(R.id.et_params);
        btnSend = view.findViewById(R.id.btn_send);
        btnCancel = view.findViewById(R.id.btn_cancel);

        btnSend.setOnClickListener(v -> {
            String command = etCommand.getText().toString().trim();
            String paramsStr = etParams.getText().toString().trim();

            if (command.isEmpty()) {
                etCommand.setError("命令不能为空");
                return;
            }

            try {
                // 验证参数是否为有效的 JSON
                new JSONObject(paramsStr);
                sendCommand(command, paramsStr);
                dismiss();
            } catch (Exception e) {
                etParams.setError("参数格式错误，请输入有效的 JSON");
            }
        });

        btnCancel.setOnClickListener(v -> dismiss());
    }

    private void sendCommand(String command, String params) {
        RetrofitClient.getInstance().getApiService().sendCommand(deviceId, command, params)
            .enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(getContext(), "命令发送成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "命令发送失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Toast.makeText(getContext(), "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }
} 